package com.payment.common.utils;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.Base64;

import javax.imageio.ImageIO;

public class ImageUtil {

	public static void main(String[] args) throws IOException {
		// 第一个:把网络图片装换成Base64
		String netImagePath = "https://image-peopleinfo.oss-cn-beijing.aliyuncs.com/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20191213143055.jpg";
		// 下面是网络图片转换Base64的方法
		String s = NetImageToBase64(netImagePath);
		System.out.println(s);
		generateImage("data:image/png;base64,"+s, "D:/aa.png");
		String base64Str = imageToBase64Str("D:/aa.png");
		System.out.println(base64Str);

	}

	/**
	 * 网络图片转换Base64的方法
	 *
	 * @param netImagePath
	 *                
	 */
	public static String NetImageToBase64(String netImagePath) {
		ByteArrayOutputStream outputStream = null;
		try {
			// 创建URL
			URL url = new URL(netImagePath);
			BufferedImage bufferedImage = ImageIO.read(url);
			outputStream = new ByteArrayOutputStream();
			ImageIO.write(bufferedImage, "jpg", outputStream);
			return Base64.getEncoder().encodeToString(outputStream.toByteArray());
		} catch (IOException e) {
			e.printStackTrace();
			return "";
		}
	}

	/**
	 * 本地图片转base64
	 *
	 * @param netImagePath
	 *                
	 */
	public static String ImageToBase64(String imagePath) {
		try {
			File imageFile = new File(imagePath);
			FileInputStream finput = new FileInputStream(imageFile);
			// 得到图片的二进制数据，以二进制封装得到数据，具有通用性
			ByteArrayOutputStream outStream = new ByteArrayOutputStream();
			// 创建一个Buffer字符串
			byte[] buffer = new byte[1024];
			// 每次读取的字符串长度，如果为-1，代表全部读取完毕
			int len = 0;
			// 使用一个输入流从buffer里把数据读取出来
			while ((len = finput.read(buffer)) != -1) {
				// 用输出流往buffer里写入数据，中间参数代表从哪个位置开始读，len代表读取的长度
				outStream.write(buffer, 0, len);
			}
			// 关闭输入流
			finput.close();
			byte[] data = outStream.toByteArray();
			return Base64.getEncoder().encodeToString(data);
		} catch (IOException e) {
			e.printStackTrace();
			return "";
		}
	}

	/**
	 * Bse64转图片
	 * 
	 * @param imgStr
	 * @param imgFilePath
	 * @return
	 * @throws IOException
	 */
	public static boolean generateImage(String imgStr, String imgFilePath) throws IOException {
		if (imgStr == null) {
			return false;
		}
		byte[] b = Base64.getDecoder().decode(imgStr);
		OutputStream out = new FileOutputStream(imgFilePath);
		out.write(b);
		out.flush();
		out.close();
		return true;
	}

	public static String imageToBase64Str(String imgFile) {
		InputStream inputStream = null;
		byte[] data = null;
		try {
			inputStream = new FileInputStream(imgFile);
			data = new byte[inputStream.available()];
			inputStream.read(data);
			inputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 加密 换行
		// BASE64Encoder encoder = new BASE64Encoder();
		// return encoder.encode(data);
		// 加密 不换行
		return Base64.getEncoder().encodeToString(data);
	}
}
